/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.stats;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import nfse.*;
import nfse.gui.server.GUIRecordFactory;

import javax.net.SocketFactory;

public class StatsFileProcessor extends DataFileProcessor {

    @Override
    public void process() {
        try {
            System.out.println("StatsFileProcessor: Processing " + getFile().getAbsolutePath());

            int offset = getOffset();
            if (offset > 0) {
                // We have processed part of the file already.
                // Skip ahead and resume reading.
                moveToOffset();
            }

            SensorInfo info = NetFSE.getSensorInfo(getSensorID());
            GUIRecordFactory factory = (GUIRecordFactory)NetFSE.getRecordFactory(info.getToolID());

            SocketFactory sf = SocketFactory.getDefault();
            Socket s = sf.createSocket(NetFSE.STATS_SERVER_HOST, NetFSE.STATS_SERVER_PORT);
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            DataInputStream dis = new DataInputStream(s.getInputStream());
            dos.writeInt(getSensorID());

            byte[] bytes = null;
            while ((bytes = getNextRecord()) != null) {
                NetFSERecord temp = factory.generate(new String(bytes), info.getSensorID());
                if (temp != null) {
                    StatsEntry stats = factory.generateStats(temp);
                    byte[] statsBytes = stats.toString().getBytes();
                    dos.writeInt(statsBytes.length);
                    dos.write(statsBytes);
                } else {
                    // Bad record, ignore
                }
            }

            dos.writeInt(-1);
            dos.flush();
            try {
                dis.readInt();
            } catch (Exception e) {

            }
            dos.close();
            dis.close();
            s.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    
    

}
